package com.sevattal.service.leaves.controller;


import com.sevattal.common.base.result.R;
import com.sevattal.service.leaves.entity.vo.PurchaseOrderVo;
import com.sevattal.service.leaves.entity.vo.AuditOrderVo;
import com.sevattal.service.leaves.service.PurchaseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  采购主表 前端控制器
 * </p>
 *
 * @author Sevattal
 * @since 2021-08-28
 */
@Api(tags = "采购订单管理")
@RestController
@RequestMapping("/admin/leaves/purchase-order")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;


    @ApiOperation("新建采购单")
    @PostMapping("createPurchaseOrder")
    public R createPurchaseOrder(@ApiParam(value = "采购单对象",required = true) @RequestBody PurchaseOrderVo purchaseOrderVo){
        boolean result = purchaseOrderService.createPurchaseOrder(purchaseOrderVo);
        if (result) {
            return R.ok().message("创建成功");
        }else{
            return R.error().message("创建失败");
        }
    }

    @ApiOperation("修改采购单")
    @PutMapping("updatePurchaseOrder")
    public R updatePurchaseOrder(@ApiParam(value = "采购单对象",required = true) @RequestBody PurchaseOrderVo purchaseOrderVo) {
        boolean result = purchaseOrderService.updatePurchaseOrder(purchaseOrderVo);
        if (result) {
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("删除采购单")
    @DeleteMapping("deletePurchaseOrder/{id}")
    public R deletePurchaseOrder(@ApiParam(value = "采购单ID",required = true) @PathVariable String id) {
        boolean result = purchaseOrderService.deletePurchaseOrder(id);
        if (result) {
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }


    @ApiOperation("获取采购信息")
    @PostMapping("getPurchaseOrderList")
    public R getPurchaseOrderList(@ApiParam(value = "采购单对象",required =  true)  @RequestBody PurchaseOrderVo purchaseOrderVo){
        List<PurchaseOrderVo> purchaseOrderList = purchaseOrderService.getPurchaseOrderList(purchaseOrderVo);
        return R.ok().data("purchaseOrderList",purchaseOrderList);
    }

    @ApiOperation("审批采购单")
    @PostMapping("auditPurchaseOrder")
    public R auditPurchaseOrder(@ApiParam(value = "审批对象",required = true) @RequestBody AuditOrderVo auditOrderVo){
        boolean result = purchaseOrderService.auditOrder(auditOrderVo);
        if (result) {
            return R.ok().message("操作成功");
        }else {
            return R.error().message("操作失败");
        }
    }


}

